﻿// Autore: Mattia Barrasso

using System;
using RecipesManager.Data;
using White.Core;
using White.Core.InputDevices;
using White.Core.UIItems;
using White.Core.UIItems.Finders;
using White.Core.UIItems.ListBoxItems;
using White.Core.UIItems.WindowItems;

namespace RecipesManager.Specs.Lista_della_spesa
{
    class ListaSpesaBdd
    {
        protected const string ShoplistManagerTitle = "Gestione Lista della Spesa";
        protected Window MainWindow;
        protected Application Application;

        protected Button AddShoppingItem;
        protected Button RemoveShoppingItem;
        protected IUIItem ShoppingItems;

        protected ComboBox Ingredients;
        protected TextBox Quantity;
        protected ComboBox CapacityMeasures;
        protected IUIItem Shops;
        protected Button Save;
        protected Button Close;

        protected void GetSingleShoppingIngredientUi()
        {
            Ingredients = MainWindow.Get<ComboBox>("cb_ingredients");
            Quantity = MainWindow.Get<TextBox>("tb_quantity");
            CapacityMeasures = MainWindow.Get<ComboBox>("cb_measures");
            Shops = MainWindow.Get<ListView>("dg_shops");
            Save = MainWindow.Get<Button>("bt_salva");
            Close = MainWindow.Get<Button>("bt_chiudi");
        }

        protected void RefreshSingleShoppingIngredientUi()
        {
            Ingredients = MainWindow.Get<ComboBox>("cb_ingredients");
            Quantity = MainWindow.Get<TextBox>("tb_quantity");
            //CapacityMeasures = MainWindow.Get<ComboBox>("cb_measures");
            Shops = MainWindow.Get<ListView>("dg_shops");
            Save = MainWindow.Get<Button>("bt_salva");
            Close = MainWindow.Get<Button>("bt_chiudi");
        }

        protected void GetShopListUi()
        {
            AddShoppingItem = MainWindow.Get<Button>("bt_add");
            RemoveShoppingItem = MainWindow.Get<Button>("bt_delete");
            ShoppingItems = MainWindow.Get<ListView>("lw_shopList");
        }

        protected void SetUpScenario()
        {
            Manager.ClearDatabase();
            //Manager.Measures.Add("grammi", "gr", MeasureType.Weight, MeasureFamily.Metric, 1);
            //Manager.Measures.Add("PesoTest", "PT", MeasureType.Weight, MeasureFamily.Metric, 1);
            foreach (var mt in DefaultMeasures)
                Manager.Measures.Add(mt.Item1, mt.Item2, mt.Item3, mt.Item4, mt.Item5);

            Application = Application.Launch(@"..\..\..\RecipesManager\bin\x86\WhiteTesting\RecipesManager.exe");
            MainWindow = Application.GetWindow("Recipes Manager");
        }

        protected void TearDownScenario()
        {
            Manager.ClearCache();
            Manager.ClearDatabase();
            Application.Kill();
        }

        protected void NavigateToMainMenu()
        {
            MainWindow.Get<Button>("bt_back").Click();
            MainWindow = Application.GetWindow("Recipes Manager");
        }

        protected void NavigateToShopList()
        {
            MainWindow.Get<Button>("listaspesa").Click();
            MainWindow = Application.GetWindow(ShoplistManagerTitle);
        }

        protected void SelectItem(string ingredient)
        {
            var lw = (ListView)ShoppingItems;
            int i;
            for (i = 0; i < lw.Rows.Count; i++)
            {
                var element = lw.Rows[i].GetElement(SearchCriteria.ByText(ingredient));
                if (element == null) continue;
                //Mouse.instance.Location = lw.Rows[i].GetElement(SearchCriteria.All).GetClickablePoint();
                Mouse.instance.Location = element.GetClickablePoint();
                Mouse.instance.Click();
            }
        }

        protected bool ShoppingListContainsIngredient(string ingredient)
        {
            var lw = (ListView)ShoppingItems;
            int i;
            for (i = 0; i < lw.Rows.Count; i++)
            {
                var element = lw.Rows[i].GetElement(SearchCriteria.ByText(ingredient));
                if (element != null) return true;
            }
            return false;
        }

        protected bool ShoppingListContains(string ingredient, string quantity)
        {
            var lw = (ListView)ShoppingItems;
            int i;
            for (i = 0; i < lw.Rows.Count; i++)
            {
                var element = lw.Rows[i].GetElement(SearchCriteria.ByText(ingredient));
                if (element != null)
                {
                    return lw.Rows[i].GetElement(SearchCriteria.ByText(quantity)) != null;
                }
            }
            return false;
        }

        protected bool ShopsListContains(string shop)
        {
            var lw = (ListView)Shops;
            int i;
            for (i = 0; i < lw.Rows.Count; i++)
            {
                var element = lw.Rows[i].GetElement(SearchCriteria.ByText(shop));
                if (element != null) return true;
            }
            return false;
        }

        protected bool ShopsListContainsSuggested(string shop)
        {
            var lw = (ListView)Shops;
            int i;
            for (i = 0; i < lw.Rows.Count; i++)
            {
                var element = lw.Rows[i].GetElement(SearchCriteria.ByText(shop));
                if (element != null)
                {
                    return lw.Rows[i].GetElement(SearchCriteria.ByText("Buono")) != null;
                }
            }
            return false;
        }



        readonly Tuple<string, string, MeasureType, MeasureFamily, double>[] DefaultMeasures = new[] 
        {
            // None
            Tuple.Create("Quanto Basta", "q.b.", MeasureType.None, MeasureFamily.None, 1.0),
            // Unit
            Tuple.Create("Unità", "", MeasureType.Unit, MeasureFamily.None, 1.0),
            // Volume - USA
            Tuple.Create("Tazza", "cup", MeasureType.Volume, MeasureFamily.USA, 0.237),           
            Tuple.Create("Cucchiaio", "tbsp", MeasureType.Volume, MeasureFamily.USA, 0.015),
            Tuple.Create("Cucchiaino", "tsp", MeasureType.Volume, MeasureFamily.USA, 0.005),
            Tuple.Create("Gallone (Usa)", "gal", MeasureType.Volume, MeasureFamily.USA, 3.785412),
            Tuple.Create("Gill (Usa)", "gill", MeasureType.Volume, MeasureFamily.USA, 0.1182941),
            Tuple.Create("Oncia Liquida (Usa)", "fl oz", MeasureType.Volume, MeasureFamily.USA, 0.02957353),
            Tuple.Create("Pinta (Usa)", "pt", MeasureType.Volume, MeasureFamily.USA, 0.4731765),
            Tuple.Create("Quarto (Usa)", "qt", MeasureType.Volume, MeasureFamily.USA, 0.9463529),           
            // Volume - UK           
            Tuple.Create("Gallone (Uk)", "gal", MeasureType.Volume, MeasureFamily.UK, 4.546),
            Tuple.Create("Gill (Uk)", "gill", MeasureType.Volume, MeasureFamily.UK, 0.142),
            Tuple.Create("Oncia Liquida (Uk)", "fl oz", MeasureType.Volume, MeasureFamily.UK, 0.0284),
            Tuple.Create("Pinta (Uk)", "pt", MeasureType.Volume, MeasureFamily.UK, 0.568),
            Tuple.Create("Quarto (Uk)", "qt", MeasureType.Volume, MeasureFamily.UK, 1.1364),          
            // Volume - Metric
            Tuple.Create("Litri", "l", MeasureType.Volume, MeasureFamily.Metric, 1.0),
            Tuple.Create("Centilitri", "cl", MeasureType.Volume, MeasureFamily.Metric, 0.01),
            Tuple.Create("Decilitri", "dl", MeasureType.Volume, MeasureFamily.Metric, 0.1),         
            Tuple.Create("Millilitri", "ml", MeasureType.Volume, MeasureFamily.Metric, 0.001),
            // Weight - USA
            Tuple.Create("Grano (Usa)", "grs", MeasureType.Weight, MeasureFamily.USA, 0.064799),
            Tuple.Create("Dramma (Usa)", "dr", MeasureType.Weight, MeasureFamily.USA, 1.7718),
            Tuple.Create("Libbra (Usa)", "lb", MeasureType.Weight, MeasureFamily.USA, 453.59237),
            Tuple.Create("Oncia (Usa)", "oz", MeasureType.Weight, MeasureFamily.USA, 28.349523),
            // Weight - UK
            Tuple.Create("Grano (Uk)", "grs", MeasureType.Weight, MeasureFamily.UK, 0.064799),
            Tuple.Create("Dramma (Uk)", "dr", MeasureType.Weight, MeasureFamily.UK, 3.89),
            Tuple.Create("Libbra (Uk)", "lb", MeasureType.Weight, MeasureFamily.UK, 453.59237),
            Tuple.Create("Oncia (Uk)", "oz", MeasureType.Weight, MeasureFamily.UK, 28.349523),
            // Weight - Metric
            Tuple.Create("Grammi", "gr", MeasureType.Weight, MeasureFamily.Metric, 1.0),
            Tuple.Create("Centigrammi", "cg", MeasureType.Weight, MeasureFamily.Metric, 0.01),
            Tuple.Create("Chilogrammi", "kg", MeasureType.Weight, MeasureFamily.Metric, 1000.0),
            Tuple.Create("Decigrammi", "dg", MeasureType.Weight, MeasureFamily.Metric, 0.1),
            Tuple.Create("Milligrammi", "mg", MeasureType.Weight, MeasureFamily.Metric, 0.001)
        };
    }
}
